/**
 * 
 */
package com.witframework.core;

import java.io.Serializable;

import org.springframework.web.context.WebApplicationContext;

import com.witframework.WitException;
import com.witframework.core.logic.ISimpleBaseService;
import com.witframework.core.logic.SimpleBaseService;
import com.witframework.core.persistence.IBaseDao;
import com.witframework.core.persistence.SimpleHibernateDao;
import com.witframework.web.mvc.WebContext;

/**
 * @author wuxinyang Jun 30, 2009 1:50:20 PM
 */
public class WitFrameWork implements Serializable {
	private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();

	public static WebContext getContext() {
		return (WebContext) threadLocal.get();
	}

	public static void setContext(WebContext webContext) {
		threadLocal.set(webContext);
	}

	public static void cleanContext() {
		threadLocal.set(null);
	}

	public static IBaseDao getBaseHibernateDao(Class entityClass) {		
		return getBaseHibernateDao(entityClass,false);
	}
	public static IBaseDao getBaseHibernateDao(Class entityClass,boolean noMem) {
		SimpleHibernateDao dao = new SimpleHibernateDao();
		dao.setEntityClass(entityClass,noMem);
		return dao;
	}
	public static IBaseDao getBaseHibernateDao(String entityClassName) {
		return getBaseHibernateDao(entityClassName,false);
	}
	public static IBaseDao getBaseHibernateDao(String entityClassName,boolean noMem) {
		try {
			return getBaseHibernateDao(Class.forName(entityClassName),noMem);
		} catch (ClassNotFoundException e) {
			throw new WitException(e);
		}
	}

	public static ISimpleBaseService getSimpleBaseService(Class entityClass) {		
		return getSimpleBaseService(entityClass,false);
	}
	public static ISimpleBaseService getSimpleBaseService(Class entityClass,boolean noMem) {
		ISimpleBaseService service=null;		
		service = new SimpleBaseService();
		service.init(entityClass,noMem);
		return service;
		
	}
	public static ISimpleBaseService getSimpleBaseService(String entityClassName) {
		return getSimpleBaseService(entityClassName,false);
	}
	public static ISimpleBaseService getSimpleBaseService(String entityClassName,boolean noMem) {
		try{			
			return getSimpleBaseService(Class.forName(entityClassName),noMem);
		} catch (ClassNotFoundException e) {
			throw new WitException(e);
		}

	}
	public static Object getBean(String beanName) {
		return BeanFactory.getBean(beanName);
	}
	public static WebApplicationContext getWebApplicationContext(){		
		return BeanFactory.getWebApplicationContext();
	}

}
